package com.jxauaia.contest.AI;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * AI响应处理器
 */
public class AIResponseProcessor {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 从星火API响应中提取AI回答
     */
    public static String extractAnswer(String response) {
        try {
            JsonNode rootNode = objectMapper.readTree(response);
            JsonNode payload = rootNode.path("payload");
            JsonNode choices = payload.path("choices");
            JsonNode text = choices.path("text");
            
            if (text.isArray() && text.size() > 0) {
                JsonNode firstText = text.get(0);
                return firstText.path("content").asText();
            }
            
            return "无法解析AI回答";
        } catch (Exception e) {
            e.printStackTrace();
            return "解析AI回答时出错";
        }
    }
    
    /**
     * 检查问题是否与比赛相关
     * @param question 用户问题
     * @return 是否与比赛相关
     */
    public static boolean isContestRelated(String question) {
        // 定义与比赛相关的关键词
        String[] keywords = {
            "比赛", "竞赛", "大赛", "赛事", "参赛", "报名", "奖项", "获奖", 
            "规则", "流程", "时间", "地点", "评分", "评审", "作品", "提交",
            "团队", "个人", "组队", "选手", "参与", "要求", "条件", "资格"
        };
        
        // 检查问题中是否包含关键词
        String lowerQuestion = question.toLowerCase();
        for (String keyword : keywords) {
            if (lowerQuestion.contains(keyword)) {
                return true;
            }
        }
        
        return false;
    }
}
